//
//  U9NewsManager.m
//  U9WatchGame
//
//  Created by uuu9 on 2017/4/18.
//  Copyright © 2017年 Jason. All rights reserved.
//

#import "U9NewsManager.h"
#import "U9NewsModel.h"
#import "U9NewsChannelModel.h"
#import "U9NewsDetailModel.h"

@implementation U9NewsManager

//获取游戏类型内某个游戏频道的新闻列表
+ (void)getNewsWithGameID:(NSString *)gid andPageNum:(NSString *)pageNum andCompletionHandle:(void (^)(U9NewsModel *model, NSError *error))complete{
    PlistStorage *plist = [PlistStorage sharedPlistStorage];
    [self POST:[NSString stringWithFormat:@"%@%@?page=%@&gid=%@&num=50",Api_URL,kGetHotPointNews,pageNum,gid]
    parameters:nil
completionHandler:^(id responseObj, NSError *error) {
    U9NewsModel *model = [U9NewsModel mj_objectWithKeyValues:responseObj];
    if (model.error_code != 0) {
        error = [self createError:model.error_msg];
        model = [U9NewsModel mj_objectWithKeyValues:[plist readFromPlist:@"NewsList"]];
    } else if (error) {
        model = [U9NewsModel mj_objectWithKeyValues:[plist readFromPlist:@"NewsList"]];
    } else {
        [plist storageToPlist:responseObj andFileName:@"NewsList"];
    }
    complete(model,error);
}];
}



//获取游戏频道为所有时，里面的频道
+ (void)getNewsChannelWithCompletionHandle:(void(^)(U9NewsChannelModel *model, NSError *error))complete{
    [self POST:[NSString stringWithFormat:@"%@%@",Api_URL,kNewsChannel]
   parameters:nil
completionHandler:^(id responseObj, NSError *error) {
    PlistStorage *plist = [PlistStorage sharedPlistStorage];
    U9NewsChannelModel *model = [U9NewsChannelModel mj_objectWithKeyValues:responseObj];
    if (model.error_code != 0) {
        error = [self createError:model.error_msg];
        model = [U9NewsChannelModel mj_objectWithKeyValues:[plist readFromPlist:@"NewsChannel"]];
    } else if (error) {
        model = [U9NewsChannelModel mj_objectWithKeyValues:[plist readFromPlist:@"NewsChannel"]];
    } else {
        [plist storageToPlist:responseObj andFileName:@"NewsChannel"];
    }
    complete(model,error);
    
}];
}

//获取游戏频道不为所有时，除了推荐以外，其他的url分类
+ (void)getURLChannelWithGid:(NSString *)gid andCompletionHandle:(void(^)(U9NewsChannelModel *model, NSError *error))complete{
    [self POST:[NSString stringWithFormat:@"%@%@?gid=%@",Api_URL,kNewsChannelForNew,gid]
   parameters:nil
completionHandler:^(id responseObj, NSError *error) {
    PlistStorage *plist = [PlistStorage sharedPlistStorage];
    U9NewsChannelModel *model = [U9NewsChannelModel mj_objectWithKeyValues:responseObj];
    if (model.error_code != 0) {
        error = [self createError:model.error_msg];
        model = [U9NewsChannelModel mj_objectWithKeyValues:[plist readFromPlist:@"NewsChannel"]];
    } else if (error) {
        model = [U9NewsChannelModel mj_objectWithKeyValues:[plist readFromPlist:@"NewsChannel"]];
    } else {
        [plist storageToPlist:responseObj andFileName:@"NewsChannel"];
    }
    complete(model,error);
    
}];
}


+ (void)getNewsDetailWithNewsID:(NSString *)newsid andGameID:(NSString *)gameid andCompletionHandle:(void (^)(U9NewsDetailModel *model, NSError *error))complete{
    [self GET:[NSString stringWithFormat:@"%@%@?articleid=%@&gameid=%@",News_URL,kGetNewsInfo,newsid,gameid]
   parameters:nil
completionHandler:^(id responseObj, NSError *error) {
    U9NewsDetailModel *model = [U9NewsDetailModel mj_objectWithKeyValues:responseObj];
    if (model.error_code != 0) {
        error = [self createError:model.error_msg];
    }
    complete(model,error);
}];
}

@end
